package com.link.base.base.urlinfo.service;

import javax.annotation.Resource;

import com.link.base.user.model.CoreUser;
import com.link.base.base.urlinfo.model.UrlDutyLink;
import com.link.base.base.urlinfo.model.UrlInfo;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import com.link.base.base.urlinfo.dao.mybatis.mapper.AppUrlDutyLinkMapper;
import com.link.base.base.urlinfo.model.AppUrlDutyLink;
import org.springframework.stereotype.Service;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * app职责与URL-app职责与URL关联关系
 *
 * @author 郭闪
 * @date 2019/08/23 11:28
 */
@Service
public class AppUrlDutyLinkServiceImpl extends BasicServiceImpl<AppUrlDutyLink> implements AppUrlDutyLinkService {

    @Resource
    private AppUrlDutyLinkMapper appUrlDutyLinkMapper;

    @Resource
    private UrlInfoService urlInfoService;

    @Override
    public BasicMapper<AppUrlDutyLink> getBasicMapper() {
        return appUrlDutyLinkMapper;
    }

    @Override
    public void batchAddUrl(List<String> urls, Long dutyId) throws Exception {
        // 查询输入的url在url表中维护的信息
        List<UrlInfo> list = urlInfoService.listInUrlInfo(urls);
        List<String> dbUrlList = list.stream().map(UrlInfo::getUrl).collect(Collectors.toList());
        // 判断输入url是否存在未在url表中维护的情况，若存在则给出提示
        List<String> undefinedUrls = urls.stream().filter(item -> !dbUrlList.contains(item)).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(undefinedUrls)) {
            // 为防止提示信息过长，最多展示3个未维护的url
            String message = undefinedUrls.size() > 3 ? String.join(",", undefinedUrls.subList(0, 3)) + ",..."
                    : String.join(",", undefinedUrls);
            throw new ServiceException("URIINFO-003", message);
        }
        List<Long> urlIds = list.stream().map(UrlInfo::getId).collect(Collectors.toList());
        // 查询输入url中已存在于数据库中的记录并借此分离待插入的数据
        List<Long> dbIds = appUrlDutyLinkMapper.existUrls(urlIds, dutyId);
        List<Long> insertIdList;
        if (CollectionUtils.isEmpty(dbIds)) {
            insertIdList = urlIds;
        } else {
            // 将输入url中已存在于中间表的记可访问设置为'Y'
            appUrlDutyLinkMapper.enableLinks(dbIds, dutyId);
            insertIdList = urlIds.stream().filter(item -> !dbIds.contains(item)).collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(insertIdList)) {
            return;
        }
        // 批量插入未关联的url数据
        List<UrlDutyLink> insertLinkList = new ArrayList<>();
        UrlDutyLink link;
        CoreUser user = UserUtil.getUser();
        for (Long item : insertIdList) {
            link = new UrlDutyLink();
            link.setId(this.keyGenerateService.keyGenerate());
            link.setCreatedBy(user.getId());
            link.setLastUpdatedBy(user.getId());
            link.setCorpid(user.getLoginCorpId());
            link.setUrlId(item);
            link.setDutyId(dutyId);
            link.setAccessFlag("Y");
            insertLinkList.add(link);
        }
        appUrlDutyLinkMapper.batchAdd(insertLinkList);

    }
}

